home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / scbench.arc / SCFILEIO.C < prev    next >
Text File  |  1980-01-01  |  9KB  |  479 lines

  1. /*
  2. ** BYTE File I/O Benchmark
  3. ** Version 1 for 8088/8086/80286/80386
  4. ** March 1988
  5. ** Written in BYTE Small-C
  6. ** Based on Small-C by J.E. Hendrix
  7. **
  8. ** Operation:
  9. ** 1. Create N files of predetermined size
  10. ** 2. Extend those N files a set amount
  11. ** 3. Enter a loop, executing:
  12. **    (a) 3 reads
  13. **    (b) 1 write
  14. **    Each read/write operation is performed on a random
  15. **    number of bytes.
  16. ** 4. Report:
  17. **    (a) Accumulated seek time
  18. **    (b) Accumulated read time and total bytes read
  19. **    (c) Accumulated write time and total bytes written.
  20. ** 5. Exit
  21. **
  22. */
  23.  
  24. #include stdio.h
  25.  
  26. int tblock[4];        /* Timer block */
  27. int rtblock[4];        /* Timer block for read time */
  28. int wtblock[4];        /* Timer block for write time */
  29. int stblock[4];        /* Timer block for seek time */
  30.  
  31. int twbytes[2];        /* Total bytes written */
  32. int trbytes[2];        /* Total bytes read */
  33.  
  34. int seed;        /* Seed for random number generator */
  35.  
  36. #define BUFSIZ 8000
  37. char *tstring;        /* Pointer to string to write */
  38. char *rbuff;        /* Buffer for reading */
  39.  
  40. /*
  41. ** COUNT defines the number of times the read-read-read-write
  42. ** loop is performed.
  43. */
  44.  
  45. #define COUNT 120
  46.  
  47. main()
  48.  
  49. {
  50.     int i;
  51.  
  52.     /* Init the seed */
  53.     seed=0;
  54.  
  55.     /* Announce yourself */
  56.     printf("BYTE File I/O Benchmark\n\n");
  57.  
  58.     /* First build tstring */
  59.  
  60.     tstring=malloc(BUFSIZ);        /* Get memory */
  61.     if(tstring==NULL) {
  62.       printf("No memory for tstring\n");
  63.       exit(0);
  64.     }
  65.     for(i=0;i<BUFSIZ;++i)
  66.         tstring[i]='A';        /* Load it up */
  67.  
  68.     rbuff=malloc(BUFSIZ);        /* Get memory */
  69.     if(rbuff==NULL) {
  70.       printf("No memory for rbuff\n");
  71.       exit(0);
  72.     }
  73.  
  74.     /* Make sure all accumulators are empty */
  75.  
  76.     for(i=0;i<4;++i)
  77.         rtblock[i]=stblock[i]=wtblock[i]=0;
  78.  
  79.     twbytes[0]=twbytes[1]=0;
  80.     trbytes[0]=trbytes[1]=0;
  81.  
  82.     /* Create the files */
  83.     printf("...Creating files\n");
  84.     for(i=0;i<10;++i)
  85.         kreate(i);
  86.  
  87.     /* Extend them */
  88.     printf("...Extending files\n");
  89.     for(i=0;i<10;++i)
  90.         appnd(i);
  91.  
  92.     /* Do the random I/O stuff */
  93.     printf("...Performing random reads/writes\n");
  94.     dorands();
  95.  
  96.     /* Tell user what happened */
  97.     report();
  98.  
  99.     exit(0);
  100. }
  101.  
  102. /*
  103. ** kreate(n)
  104. ** Create file n.  Write the assigned number of bytes.
  105. ** Close the file.
  106. ** This routine accumulates the number of bytes written
  107. ** and the time to write them.
  108. */
  109. kreate(n) int n;
  110. {
  111.     int fd;        /* File handle */
  112.     int nb;        /* Number of bytes to write */
  113.  
  114.     switch(n) {
  115.  
  116.       case 0: 
  117.           nb=4000;
  118.            break;
  119.       case 1: 
  120.           nb=10000;
  121.           break;
  122.       case 2: 
  123.           nb=500;
  124.           break;
  125.       case 3: 
  126.           nb=2800;
  127.           break;
  128.       case 4: 
  129.           nb=25000;
  130.           break;
  131.       case 5: 
  132.           nb=14000;
  133.           break;
  134.       case 6: 
  135.           nb=8000;
  136.           break;
  137.       case 7: 
  138.           nb=8800;
  139.           break;
  140.       case 8: 
  141.           nb=300;
  142.           break;
  143.       case 9: 
  144.           nb=21111;
  145.           break;
  146.     }
  147.  
  148.     fd=fopen(gfname(n),"w");    /* Open the file */
  149.     accwbytes(nb);        /* Accumulate total bytes written */
  150.     while(nb!=0) {
  151.       if(nb>4000) n=4000; else n=nb;
  152.       gtime(tblock);        /* Turn on timer */
  153.       write(fd,tstring,n);
  154.       calctim(tblock);    /* Turn off the timer */
  155.           accwtime();        /* Accumulate total write time */
  156.       nb-=n;
  157.     }
  158.     fclose(fd);        /* Close the file */
  159.     return;
  160. }
  161. /*
  162. ** appnd(n)
  163. ** Open nth file.  Append the appropriate number of bytes
  164. */
  165. appnd(n)
  166. int n;
  167. {
  168.  
  169.     int fd;        /* File handle */
  170.     int nb;        /* Number of bytes */
  171.  
  172.     switch(n) {
  173.  
  174.       case 0:  nb=12000;
  175.            break;
  176.       case 1:  nb=20300;
  177.            break;
  178.       case 2:  nb=31111;
  179.            break;
  180.       case 3:  nb=3400;
  181.            break;
  182.       case 4:  nb=9099;
  183.            break;
  184.       case 5:  nb=20755;
  185.            break;
  186.       case 6:  nb=7000;
  187.            break;
  188.       case 7:  nb=400;
  189.            break;
  190.       case 8:  nb=22000;
  191.            break;
  192.       case 9:  nb=27000;
  193.            break;
  194.     }
  195.  
  196.     fd=fopen(gfname(n),"a");    /* Open in append mode */
  197.     accwbytes(nb);        /* Accumulate total bytes written */
  198.     while(nb!=0) {
  199.       if(nb>4000) n=4000; else n=nb;
  200.       gtime(tblock);        /* Turn on timer */
  201.       write(fd,tstring,n);
  202.       calctim(tblock);    /* Turn off the timer */
  203.           accwtime();        /* Accumulate total write time */
  204.       nb-=n;
  205.     }
  206.     fclose(fd);
  207.     return;
  208. }
  209.  
  210. /*
  211. ** dorands()
  212. ** Do the random reads and writes.  Accumulating as you go.
  213. **
  214. */
  215. dorands()
  216. {
  217.     int i,j,nb;
  218.     int fd,offsetl,offseth;
  219.  
  220. /* Do the whole thing COUNT times */
  221.  
  222.     for(i=0;i<COUNT;++i)
  223.     {
  224.  
  225.       /* For each COUNT, do 3 reads, 1 write */
  226.       for(j=0;j<3;++j)
  227.       {
  228.         fd=fopen(gfname(randwc(10)),"r");
  229.         seek(fd,0,0,2);
  230.         tell(fd,&offsetl,&offseth);    /* Get file len */
  231.         if(offseth!=0) offseth=randwc(offseth);
  232.         if(offsetl<0)
  233.             offsetl=seed=rand(seed);
  234.         else
  235.             offsetl=randwc(offsetl);
  236.         gtime(tblock);
  237.         seek(fd,offsetl,offseth,0);
  238.         calctim(tblock);
  239.         accstime();        /* Accumulate seek time */
  240.         nb=randwc(8000);    /* Bytes to read */
  241.         gtime(tblock);
  242.         nb=read(fd,rbuff,nb);
  243.         calctim(tblock);
  244.         if(nb==ERR) {
  245.           printf("Error reading\n");
  246.           exit(0);
  247.         }
  248.         accrbytes(nb);        /* Accumulate bytes read */
  249.         accrtime();
  250.         fclose(fd);
  251.     }
  252.         fd=fopen(gfname(randwc(10)),"u");
  253.         seek(fd,0,0,2);
  254.         tell(fd,&offsetl,&offseth);    /* Get file len */
  255.         if(offseth!=0) offseth=randwc(offseth);
  256.         if(offsetl<0)
  257.             offsetl=seed=rand(seed);
  258.         else
  259.             offsetl=randwc(offsetl);
  260.         gtime(tblock);
  261.         seek(fd,offsetl,offseth,0);
  262.         calctim(tblock);
  263.         accstime();        /* Accumulate seek time */
  264.         nb=randwc(8000);    /* Bytes to write */
  265.         gtime(tblock);
  266.         nb=write(fd,tstring,nb);
  267.         calctim(tblock);
  268.         if(nb==ERR) {
  269.           printf("Error writing\n");
  270.           exit(0);
  271.         }
  272.         accwbytes(nb);        /* Accumulate bytes written */
  273.         accwtime();
  274.         fclose(fd);
  275.     }
  276.     return;
  277. }
  278.  
  279. /*
  280. ** report()
  281. ** Report results
  282. */
  283. report()
  284. {
  285.  
  286.     printf("***Results:  (All times are HH:MM:SS:1/100)\n\n");
  287.  
  288.     printf(" Total seek time: %d:%d:%d:%d\n\n",stblock[0],
  289.          stblock[1],stblock[2],stblock[3]);
  290.  
  291.     printf(" Total read time: %d:%d:%d:%d\n\n",rtblock[0],
  292.          rtblock[1],rtblock[2],rtblock[3]);
  293.     printf("            bytes:");
  294.  
  295.     writebig(trbytes);
  296.     printf("\n\n");
  297.  
  298.     printf(" Total write time: %d:%d:%d:%d\n\n",wtblock[0],
  299.          wtblock[1],wtblock[2],wtblock[3]);
  300.     printf("             bytes:");
  301.  
  302.     writebig(twbytes);
  303.     printf("\n\n");
  304.  
  305.     printf("P.S. Don't forget to delete the files.\n");
  306.  
  307. }
  308.  
  309.  
  310. /*
  311. ** Take the time interval stored in tblock[] and add that to
  312. ** the array holding total accumulated write time (wtblock[]).
  313. */
  314. accwtime()
  315. {
  316.     wtblock[3]+=tblock[3];    /* Hundredths */
  317.     if(wtblock[3]>=100) {
  318.         wtblock[2]+=1;
  319.         wtblock[3]-=100;
  320.     }
  321.     wtblock[2]+=tblock[2]; /* Seconds */
  322.     if(wtblock[2]>=60) {
  323.         wtblock[1]+=1;
  324.         wtblock[2]-=60;
  325.     }
  326.     wtblock[1]+=tblock[1]; /* Minutes */
  327.     if(wtblock[1]>=60) {
  328.         wtblock[0]+=1;
  329.         wtblock[1]-=60;
  330.     }
  331.     wtblock[0]+=tblock[0];    /* Hours */
  332.     return;
  333. }
  334.  
  335. /*
  336. ** Take the time interval stored in tblock[] and add that to
  337. ** the array holding total accumulated read time (rtblock[]).
  338. */
  339. accrtime()
  340. {
  341.     rtblock[3]+=tblock[3];    /* Hundredths */
  342.     if(rtblock[3]>=100) {
  343.         rtblock[2]+=1;
  344.         rtblock[3]-=100;
  345.     }
  346.     rtblock[2]+=tblock[2]; /* Seconds */
  347.     if(rtblock[2]>=60) {
  348.         rtblock[1]+=1;
  349.         rtblock[2]-=60;
  350.     }
  351.     rtblock[1]+=tblock[1]; /* Minutes */
  352.     if(rtblock[1]>=60) {
  353.         rtblock[0]+=1;
  354.         rtblock[1]-=60;
  355.     }
  356.     rtblock[0]+=tblock[0];    /* Hours */
  357.     return;
  358. }
  359.  
  360. /*
  361. ** Take the time interval stored in tblock[] and add that to
  362. ** the array holding total accumulated seek time (rtblock[]).
  363. */
  364. accstime()
  365. {
  366.     stblock[3]+=tblock[3];    /* Hundredths */
  367.     if(stblock[3]>=100) {
  368.         stblock[2]+=1;
  369.         stblock[3]-=100;
  370.     }
  371.     stblock[2]+=tblock[2]; /* Seconds */
  372.     if(stblock[2]>=60) {
  373.         stblock[1]+=1;
  374.         stblock[2]-=60;
  375.     }
  376.     stblock[1]+=tblock[1]; /* Minutes */
  377.     if(stblock[1]>=60) {
  378.         stblock[0]+=1;
  379.         stblock[1]-=60;
  380.     }
  381.     stblock[0]+=tblock[0];    /* Hours */
  382.     return;
  383. }
  384.  
  385. /*
  386. ** accwbytes(n)
  387. ** Add n to total accumulated bytes written array.
  388. */
  389. accwbytes(n)
  390. int n;
  391. {
  392.     while(n>10000) {
  393.         twbytes[0]+=1;
  394.         n-=10000;
  395.     }
  396.     twbytes[1]+=n;
  397.     if(twbytes[1]>=10000) {
  398.         twbytes[0]+=1;
  399.         twbytes[1]-=10000;
  400.     }
  401.     return;
  402. }
  403.  
  404. /*
  405. ** accrbytes(n)
  406. ** Add n to total accumulated bytes read array.
  407. */
  408. accrbytes(n)
  409. int n;
  410. {
  411.     while(n>10000) {
  412.         trbytes[0]+=1;
  413.         n-=10000;
  414.     }
  415.     trbytes[1]+=n;
  416.     if(trbytes[1]>=10000) {
  417.         trbytes[0]+=1;
  418.         trbytes[1]-=10000;
  419.     }
  420.     return;
  421. }
  422.  
  423. /*
  424. ** gfname(n)
  425. ** Get the nth file's filename (returned as pointer).
  426. ** Note that n is 0-based.
  427. */
  428. gfname(n)
  429. int n;
  430. {
  431.     switch(n) {
  432.       case 0: return("FILE01.DAT");
  433.       case 1: return("FILE02.DAT");
  434.       case 2: return("FILE03.DAT");
  435.       case 3: return("FILE04.DAT");
  436.       case 4: return("FILE05.DAT");
  437.       case 5: return("FILE06.DAT");
  438.       case 6: return("FILE07.DAT");
  439.       case 7: return("FILE08.DAT");
  440.       case 8: return("FILE09.DAT");
  441.       case 9: return("FILE10.DAT");
  442.     }
  443. }
  444.  
  445. /*
  446. ** randwc(x)
  447. ** Get a random number (positive) with ceiling x.
  448. */
  449. randwc(x)
  450. int x;
  451. {
  452.  
  453.     if(x==0) return(0);
  454.     seed=rand(seed);    /* Get a random number */
  455.     return(abs(seed)%x);
  456. }
  457.  
  458. /*
  459. ** writebig(block)
  460. ** Write a big number.  number=10000*block[0]+block[1]
  461. */
  462. writebig(block) int block[];
  463. {
  464.     printf("%d",block[0]);
  465.     if(block[1]>999) printf("%u",block[1]);
  466.     else {
  467.      printf("0");
  468.      if(block[1]>99) printf("%u",block[1]);
  469.      else {
  470.       printf("0");
  471.       if(block[1]>9) printf("%u",block[1]);
  472.       else {
  473.        printf("0%u",block[1]);
  474.       }
  475.      }
  476.     }
  477.   return;
  478. }
  479.